<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Employee extends PNC_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model(array('mod_employee', 'mod_department', 'mod_option'));
	}

	/*
	 * Index
	 * @access public
	 * @return void
	 */

	public function index() {
		redirect('employee/show_employee/');
	}

	/*
	 * Show Employee
	 * @access public
	 */

	public function show_employee() {
		if ($this->check_session()) {
			$this->session->set_userdata("cur_page", $this->uri->segment(3));
			$this->session->set_userdata("pagination", $this->uri->segment(4));
			$data['title'] = 'Employee';
			$data['menu'] = $this->mod_menu->show_menu();
			$num_per_page = 10;
			/* in case user delete query string */
			if ($this->session->userdata('per_page')) {
				$this->session->unset_userdata('per_page');
			}
			/* get limit per page from query string */
			if ($this->input->get('limit', TRUE)) {
				$this->session->set_userdata('per_page', $this->input->get('limit', TRUE));
				$num_per_page = $this->session->userdata('per_page');
			}
			$config['total_rows'] = $this->mod_employee->count_employee(); // initialize total employee
			$config['base_url'] = site_url("employee/show_employee/"); // initialize base url
			$config['per_page'] = $num_per_page;
			$config['uri_segment'] = 3;
			$choice = $config["total_rows"] / $config["per_page"];
			$config["num_links"] = round($choice);
			$config['first_link'] = 'First';
			$config['next_link'] = 'Next';
			$config['prev_link'] = 'Pre';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config); //function to show all pages
			$page = ($this->uri->segment($config['uri_segment']) && $this->uri->segment($config['uri_segment']) > 0) ? $this->uri->segment($config['uri_segment']) : 0;
			$data['employee'] = $this->mod_employee->show_employee($config['per_page'], $page, '');
			$data['pagination'] = $this->pagination->create_links();
			if ($this->uri->segment(3) === 'view_detail') {
				$data['get_profile'] = PNC_Model::get_profile($this->uri->segment(4));
				$this->load->view('employee/view_detail', $data);
			}
			$this->view_main($data);
		} else {
			redirect('main/');
		}
	}

	public function view_detail() {
		if ($this->check_session()) {
			$data['title'] = 'Detail of employee';
			$data['get_profile'] = PNC_Model::get_profile($this->uri->segment(3));
			$this->view_main($data);
		} else {
			redirect('main/');
		}
	}

	/*
	 * Add New Employee
	 * @access public
	 * @return void
	 */

	public function add_employee() {
		if ($this->check_session()) {
			$data['title'] = 'Add New Employee';
			$data['option_gender'] = array('' => '-- Select --', 'M' => 'Male', 'F' => 'Female');
//			$data['option_grp'] = $this->mod_option->get_group();
			$data['option_status'] = array('' => '-- Select --', '0' => 'Dispublished', '1' => 'Published');
			$data['option_pos'] = $this->mod_option->get_position();
			$data['option_role'] = $this->mod_option->get_role();
			$data['option_dept'] = $this->mod_option->get_department();
			$data['option_sub_dept'] = $this->mod_option->get_subdepartment();

			if ($this->input->post('btn_add_employee')) {
//				$this->form_validation->set_rules('txt_id', 'Id', 'trim|required|upper_alpha_numeric|is_unique[pnc_employee.emp_id]|xss_clean');
				$this->form_validation->set_rules('txt_fname', 'Frist Name', 'trim|required|alpha|xss_clean');
				$this->form_validation->set_rules('txt_lname', 'Last Name', 'trim|required|alpha|xss_clean');
//				$this->form_validation->set_message('upper_alpha_numeric', 'The Id field must be stated with STAFF_ and number at least three chatacters');
				$this->form_validation->set_message('is_unique', 'This staff id is already exist!');
				if ($this->form_validation->run() == FALSE) {
					$this->view_main($data);
				} else {
					$get_emp_id = $this->input->post('txt_id');
					$get_fname = $this->input->post('txt_fname');
					$get_lname = $this->input->post('txt_lname');
					$get_username = strtolower($get_fname) . '.' . strtolower($get_lname);
					$get_password = PNC_Controller::PASSWORD;
					$get_email = $get_username . PNC_Controller::EXT_MAIL; //Get extension of mail from PNC_Controller.php
					$get_gender = $this->input->post('txt_gender');
//					$get_group = $this->input->post('txt_group');
					$get_position = $this->input->post('txt_position');
					$get_role = $this->input->post('txt_role');
					$get_create_date = date('Y-m-d');
					$get_status = $this->input->post('txt_status');
					$get_department = $this->input->post('txt_dept');
					$get_sub_dept = $this->input->post('txt_sub_dept');
					$get_sub_dept = !$this->input->post('txt_sub_dept') ? 0 : $get_sub_dept;
					$get_start_workdate = $this->input->post('txt_start_workdate');

					$photo = $this->input->post('txt_photo');
					$config['upload_path'] = './uploads/users/';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['max_size'] = '10000';
					$this->load->library('upload', $config);
					$this->upload->initialize($config);
					if ($get_gender === '' OR $get_position === '' OR $get_role === '' OR $get_department === '' OR $get_status === '' OR $get_start_workdate === '') {
						$this->session->set_userdata('add_erro', show_message('Please complete all fields!', 'error'));
						$this->view_main($data);
					} else {/* With upload image */
						if ($this->upload->do_upload('txt_photo') || $this->upload->do_upload('txt_photo') == "") {
							$img = $this->upload->data();
//							$image = $img['orig_name'];
							$image = $img['file_name'];
							if ($image != "") {
								$this->resize_image($config['upload_path'], $image, 150, 300);
							}
							$image = $image != '' ? $image : 'default.png';
						} /* End of uploading */


						$result = $this->mod_employee->add_employee($get_emp_id, $get_fname, $get_lname, $get_username, $get_password, $get_email, $get_gender, $get_position, $get_role, $get_create_date, $get_status, $get_department, $image, $get_sub_dept, $get_start_workdate);
						if ($result > 0) {
							$this->session->set_userdata('add_employee', show_message("Added successfully", "success"));
							if ($this->session->userdata('per_page')) {
								redirect('employee/show_employee/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
							}
							redirect('employee/show_employee/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
						}
					}
				}
			} else {
				$this->view_main($data);
			}
		} else {
			redirect('main/');
		}
	}

	public function edit_employee() {
		if ($this->check_session()) {
			$data['title'] = 'Edit Employee';
			$data['option_status'] = array('' => '-- Select --', '0' => 'Dispublished', '1' => 'Published');
//			$data['option_grp'] = $this->mod_option->get_group();
			$data['option_gender'] = array('' => '-- Select --', 'M' => 'Male', 'F' => 'Female');
			$data['option_pos'] = $this->mod_option->get_position();
			$data['option_role'] = $this->mod_option->get_role();
			$data['option_dept'] = $this->mod_option->get_department();
			$data['option_sub_dept'] = $this->mod_option->get_subdepartment();
			$data['edit_emp'] = $this->mod_employee->edit_employee($this->uri->segment(3));
			foreach ($data['edit_emp']->result() as $getdata) {
				$alias_dept = $getdata->dept_alias;
				$this->session->set_userdata('get_dept_alias', $alias_dept);
			}
			$data['subdept'] = $this->mod_option->get_subdept_id($this->uri->segment(3));
			foreach ($data['subdept']->result() as $row) {
				$subdept_id = $row->sub_dept;
				$data['edit_subdept'] = $this->mod_option->get_subdept($subdept_id);
			}

			if ($this->input->post('btn_edit')) {
				$this->form_validation->set_rules('txt_fname', 'Frist Name', 'trim|required|alpha|xss_clean');
				$this->form_validation->set_rules('txt_lname', 'Last Name', 'trim|required|alpha|xss_clean');
				if ($this->form_validation->run() == FALSE) {
					$this->view_main($data);
				} else {
					/* Old value */
//                                    if($this->input->post('txt_position')){echo "Have txt_position";exit();}else{ echo "Not txt_position";  exit();}
					$old_emp_id = $this->input->post('old_id');
					$old_fname = $this->input->post('old_fname');
					$old_lname = $this->input->post('old_lname');
					$old_gender = $this->input->post('old_gender');
//					$old_group = $this->input->post('old_group');
					$old_position = $this->input->post('old_position');
					$old_role = $this->input->post('old_role');
					$old_status = $this->input->post('old_status');
					$old_department = $this->input->post('old_dept');
					$old_sub_dept = $this->input->post('old_sub_dept');
					$old_start_workdate = $this->input->post('old_start_workdate');

					/* Get New post */
					$get_emp_id = !$this->input->post('txt_id') ? $old_emp_id : $this->input->post('txt_id');
					$get_fname = !$this->input->post('txt_fname') ? $old_fname : $this->input->post('txt_fname');
					$get_lname = !$this->input->post('txt_lname') ? $old_lname : $this->input->post('txt_lname');
					$get_username = $get_fname . '.' . $get_lname;
					$get_email = $get_username . PNC_Controller::EXT_MAIL;
					$get_gender = !$this->input->post('txt_gender') ? $old_gender : $this->input->post('txt_gender');
//					$get_group = !$this->input->post('txt_group') ? $old_group : $this->input->post('txt_group');
					$get_position = !$this->input->post('txt_position') ? $old_position : $this->input->post('txt_position');
					$get_role = !$this->input->post('txt_role') ? $old_role : $this->input->post('txt_role');
					$get_status = !$this->input->post('txt_status') ? $old_status : $this->input->post('txt_status');
					$get_department = !$this->input->post('txt_dept') ? $old_department : $this->input->post('txt_dept');
					$get_sub_dept = !$this->input->post('txt_sub_dept') ? 0 : $this->input->post('txt_sub_dept');
					$get_start_workdate = !$this->input->post('txt_start_workdate') ? $old_start_workdate : $this->input->post('txt_start_workdate');
					$get_modified_date = date('Y-m-d');

					$src_img = $this->input->post('src_img');
					$config['upload_path'] = './uploads/users/';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['max_size'] = '10000';
					$this->load->library('upload', $config);
					$this->upload->initialize($config);
					if ($get_gender === '' OR $get_position === '' OR $get_role === '' OR $get_department === '' OR $get_status === '') {
						$this->session->set_userdata('edit_erro', show_message('Please complete all fields!', 'error'));
						$this->view_main($data);
					} else {/* With upload image */
						if ($this->upload->do_upload('txt_photo') || $this->upload->do_upload('txt_photo') == "") {
							$img = $this->upload->data();
							$image = $img['orig_name'];
							if ($image != "") {
								$this->resize_image($config['upload_path'], $image, 150, 300);
							}
							$image = $image != '' ? $image : $src_img;
						} /* End of uploading */
//echo $this->input->post('txt_position')." ".
//$get_position;
//exit();
						$result = $this->mod_employee->update_employee($this->session->userdata('emp_id'), $get_emp_id, $get_fname, $get_lname, $get_email, $get_username, $get_gender, $get_position, $get_role, $get_status, $get_department, $get_modified_date, $image, $get_sub_dept, $get_start_workdate);
						if ($result > 0) {
							$this->session->set_userdata('edit_success', show_message("Edit was successfully!", "success"));
							if ($this->session->userdata('per_page')) {
								redirect('employee/show_employee/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
							}
							redirect('employee/show_employee/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
						}
					}
				}
			} else {
				$this->session->set_userdata('emp_id', $this->uri->segment(3));
				$this->view_main($data);
			}
		} else {
			redirect('main/');
		}
	}

	public function delete_employees() {
		$checked_id = $this->input->post('empCheck');
		/* PNC_Controller::delete_checked($controller, $function, $model, $checked_id, $lea_type, $where, $table) */
		PNC_Controller::delete_checked('employee', 'show_employee', 'PNC_Model', $checked_id, '', 'staff_id', 'pnc_employee');
	}

	public function delete() {
		$this->session->set_userdata('get_page', $this->uri->segment(3));
		$this->session->set_userdata('get_id', $this->uri->segment(4));
		$page = $this->session->userdata('get_page');
		$id = $this->session->userdata('get_id');
		if (!$this->uri->segment(3)) {
			$page = 0;
		}
		$this->mod_employee->delete_individual($id, 'staff_id', 'pnc_employee');
		$this->session->set_userdata('delete_success', show_message("Deleted successfully!", "success"));
		if ($this->session->userdata('per_page')) {
			redirect('employee/show_employee/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
		}
		redirect('employee/show_employee/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
	}

}